package com.icim.util;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JackUtil {
	private static Logger logger = Logger.getLogger(JackUtil.class);

	public static <T> T fromJson(String jsonString, T t) {
		ObjectMapper mapper = new ObjectMapper();
		if (jsonString == null) {
			return null;
		}
		try {
			return (T) mapper.readValue(jsonString, t.getClass());
		} catch (Exception ex) {
			logger.warn("parse json string error:" + jsonString, ex);
			return null;
		}
	}

	public static String toJsonString(Object object) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.writeValueAsString(object);
		} catch (Exception e) {
			logger.warn("write to json string error:" + object, e);
			return null;
		}
	}
}
